<?php
/**
* This is a representation of a player in DB
* @author Juan G. Bernal <juancho2986@gmail.com>
* @version 1.0
* @package Player
*/
class Player
{
    private $dbinterface;

    /**
    * Constructor function
    */
    function __construct()
    {
        $this->dbinterface = new DBInterface();
    }

    /**
    * Insert a new player in th DB
    * @param Array $data An associative array with the player data
    * @return Bool Indicates if the insertion was succesful
    */
    function create($data)
    {
        return $this->dbinterface->exec_insert($data,'player');
    }

    /**
    * Modify a single field of one player
    * @param String $fld_name Field name
    * @param String $fld_value Field value
    * @param Integer $id Id of the player to be modified
    * @return Bool Indicates if the transaction was succesfully
    */
    function modify($fld_name,$fld_value,$id)
    {
        $data = array();
        if($fld_name=='pla_date_birth')
            $fld_value = str_replace(".","-",$fld_value);
        $data[$fld_name] = $fld_value;
        return $this->dbinterface->exec_update($data,'player','pla_id='.$id);
    }

    /**
    * Delete a player
    * @param Integer $id Id of the player to be deleted
    */
    function remove($id)
    {
        $this->dbinterface->exec_delete('player','pla_id='.$id);
    }

    /**
    * Retrieve the information of one player
    * @param Integer $id Id of the player
    * @return Array Array with the player data
    */
    function getData($id)
    {
        $table = array('player','delegation');
        $fields = array('pla_id','pla_document','pla_first_name','pla_last_name','pla_gender',
                        'to_char(pla_date_birth,\'YYYY.MM.DD\') as pla_date_birth','pla_del_id',
                        'pla_place_birth','pla_height','pla_weight','del_name as delegation');
        $criteria = 'pla_id='.$id.' and del_id=pla_del_id';
        $result_set = $this->dbinterface->exec_select($fields,$table,$criteria);
        return $result_set[0];
    }

    /**
    * Retrieve a list of players accord of search criteria
    * @param String $name A part or complete value to the search
    * @param String $search_type The type of search to the criteria
    * @param Integer $limit Limit clause for the SQL query, this is optional
    * @param Integer $offset Offset clause for the SQL query, this is optional
    * @return Array Bidimensional array with the players data
    */
    function getList($name='',$search_type='',$limit=0,$offset=0)
    {
        $table = array('player','delegation');
        $fields = array('pla_id','pla_first_name','pla_last_name','pla_gender','delegation.del_name as delegation');
        $criteria = "pla_del_id=del_id and del_id!=1";
        if($search_type=='first_name')
            $criteria .= " and pla_first_name ilike '%".$name."%'";
        elseif($search_type=='last_name')
            $criteria .= " and pla_last_name ilike '%".$name."%'";
        elseif($search_type=='both')
            $criteria .= " and (pla_first_name || ' ' || pla_last_name) ilike '%".$name."%'";
        elseif($search_type=='delegation')
            $criteria .= " and del_name ilike'%".$name."%'";
        $criteria .= " order by pla_id";
        if($limit!=0)
            $criteria .= " limit ".$limit." offset ".$offset;
        return $this->dbinterface->exec_select($fields,$table,$criteria);
    }

    /**
    * Obtains the number of pages when a list of players will be paginated
    * @param String $name A part or complete value to the search
    * @param String $search_type The type of search to the criteria
    * @return Integer Number of pages
    */
    function getNumPages($name='',$search_type='')
    {
        $players = $this->getList($name,$search_type);
        $rows = count($players);
        return ceil($rows/10);
    }

    /**
    * Validate if exist a player with the same input document
    * @param String $doc Input name
    * @return Bool Indicates if exists a player
    */
    function exist($doc)
    {
        $players = $this->dbinterface->exec_select(array('pla_id'),'player',"pla_document='".$doc."'");

        if($players==0)
            return false;
        else
            return true;
    }

    /**
    * Obtains a name list of players
    * @param String $name A part or complete value to the search
    * @param String $search_type The type of search to the criteria
    * @return Array The names array
    */
    function getNames($name, $search_type)
    {
        $table = 'player';
        if($search_type=='first_name')
        {
            $criteria = "pla_first_name ilike '%".$name."%'";
            $fields = array('pla_first_name as name');
        }
        elseif($search_type=='last_name')
        {
            $criteria = "pla_last_name ilike '%".$name."%'";
            $fields = array('pla_last_name as name');
        }
        elseif($search_type=='both')
        {
            $criteria = "(pla_first_name || ' ' || pla_last_name) ilike '%".$name."%'";
            $fields = array("(pla_first_name || ' ' || pla_last_name) as name");
        }
        elseif($search_type=='delegation')
        {
            $criteria = "del_name ilike'%".$name."%'";
            $fields = array('del_name as name');
            $table = 'delegation';
        }

        return $this->dbinterface->exec_select($fields,$table,$criteria,'',true);
    }

    /**
    * Obtains a place birth list of players
    * @param String $name A part or complete value to the search
    * @return Array The places array
    */
    function getPlaces($name)
    {
        $criteria = "pla_place_birth ilike '%".$name."%'";
        return $this->dbinterface->exec_select(array('pla_place_birth as name'),'player',$criteria,'',true);
    }

    /**
    * Verify if a player has an event's game associated
    * @param Integer $id Id of the player
    * @return Bool Indicates if the player has event's game
    */
    function hasEvent($id)
    {
        $criteria = "eve_pla_id=".$id;
        $events = $this->dbinterface->exec_select(array('*'),'event_game',$criteria);
        if($events==0)
            return false;
        else
            return true;
    }

    /**
    * Verify if a player has a records associated
    * @param Integer $id Id of the player
    * @return Bool Indicates if the player has records
    */
    function hasRecord($id)
    {
        $criteria = "rec_pla_id=".$id;
        $records = $this->dbinterface->exec_select(array('*'),'record',$criteria);
        if($records==0)
            return false;
        else
            return true;
    }
}
?>